<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务id" prop="tasksId">
        <el-input
          v-model="queryParams.tasksId"
          placeholder="请输入任务id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="初次学习的时间" prop="initialStudyTime">
        <el-date-picker clearable
          v-model="queryParams.initialStudyTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择初次学习的时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="第一次复习的时间" prop="reviewTime1">
        <el-date-picker clearable
          v-model="queryParams.reviewTime1"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择第一次复习的时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="第二次复习的时间" prop="reviewTime2">
        <el-date-picker clearable
          v-model="queryParams.reviewTime2"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择第二次复习的时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="第三次复习的时间" prop="reviewTime3">
        <el-date-picker clearable
          v-model="queryParams.reviewTime3"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择第三次复习的时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="第四次复习的时间" prop="reviewTime4">
        <el-date-picker clearable
          v-model="queryParams.reviewTime4"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择第四次复习的时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="第五次复习的时间" prop="reviewTime5">
        <el-date-picker clearable
          v-model="queryParams.reviewTime5"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择第五次复习的时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="复习计划的创建时间" prop="createdAt">
        <el-date-picker clearable
          v-model="queryParams.createdAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择复习计划的创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="复习计划的更新时间" prop="updatedAt">
        <el-date-picker clearable
          v-model="queryParams.updatedAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择复习计划的更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['pom:review:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['pom:review:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['pom:review:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['pom:review:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reviewList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键，唯一标识每个复习计划" align="center" prop="id" />
      <el-table-column label="类型：基础知识、进阶篇" align="center" prop="type" />
      <el-table-column label="任务id" align="center" prop="tasksId" />
      <el-table-column label="用户ID，标识哪个用户的学习计划" align="center" prop="userId" />
      <el-table-column label="学习内容的描述" align="center" prop="content" />
      <el-table-column label="初次学习的时间" align="center" prop="initialStudyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.initialStudyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="第一次复习的时间" align="center" prop="reviewTime1" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reviewTime1, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="第二次复习的时间" align="center" prop="reviewTime2" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reviewTime2, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="第三次复习的时间" align="center" prop="reviewTime3" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reviewTime3, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="第四次复习的时间" align="center" prop="reviewTime4" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reviewTime4, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="第五次复习的时间" align="center" prop="reviewTime5" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reviewTime5, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="复习计划的创建时间" align="center" prop="createdAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="复习计划的更新时间" align="center" prop="updatedAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['pom:review:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['pom:review:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改review对话框 -->
    <el-dialog :title="title" :visible.sync="open" fullscreen append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="任务id" prop="tasksId">
          <el-input v-model="form.tasksId" placeholder="请输入任务id" />
        </el-form-item>
        <el-form-item label="用户ID，标识哪个用户的学习计划" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID，标识哪个用户的学习计划" />
        </el-form-item>
        <el-form-item label="学习内容的描述">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="初次学习的时间" prop="initialStudyTime">
          <el-date-picker clearable
            v-model="form.initialStudyTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择初次学习的时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="第一次复习的时间" prop="reviewTime1">
          <el-date-picker clearable
            v-model="form.reviewTime1"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择第一次复习的时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="第二次复习的时间" prop="reviewTime2">
          <el-date-picker clearable
            v-model="form.reviewTime2"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择第二次复习的时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="第三次复习的时间" prop="reviewTime3">
          <el-date-picker clearable
            v-model="form.reviewTime3"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择第三次复习的时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="第四次复习的时间" prop="reviewTime4">
          <el-date-picker clearable
            v-model="form.reviewTime4"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择第四次复习的时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="第五次复习的时间" prop="reviewTime5">
          <el-date-picker clearable
            v-model="form.reviewTime5"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择第五次复习的时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="复习计划的创建时间" prop="createdAt">
          <el-date-picker clearable
            v-model="form.createdAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择复习计划的创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="复习计划的更新时间" prop="updatedAt">
          <el-date-picker clearable
            v-model="form.updatedAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择复习计划的更新时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="删除标记" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标记" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-divider content-position="center">recall信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddPomRecallTasks">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeletePomRecallTasks">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="pomRecallTasksList" :row-class-name="rowPomRecallTasksIndex" @selection-change="handlePomRecallTasksSelectionChange" ref="pomRecallTasks">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="任务id，最小城池" prop="tasksId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.tasksId" placeholder="请输入任务id，最小城池" />
            </template>
          </el-table-column>
          <el-table-column label="用户ID，标识哪个用户的学习计划" prop="userId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.userId" placeholder="请输入用户ID，标识哪个用户的学习计划" />
            </template>
          </el-table-column>
          <el-table-column label="初次学习的时间" prop="initialStudyTime" width="150">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.initialStudyTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择初次学习的时间" />
            </template>
          </el-table-column>

          <el-table-column label="第一次复习的效果" prop="reviewTimeRecall1" width="240">
            <template slot-scope="scope">
              <el-input type="textarea" v-model="scope.row.reviewTimeRecall1" placeholder="第一次复习的效果" />
            </template>
          </el-table-column>

          <el-table-column label="第二次复习的效果" prop="reviewTimeRecall2" width="240">
            <template slot-scope="scope">
              <el-input type="textarea" v-model="scope.row.reviewTimeRecall2" placeholder="第2次复习的效果" />
            </template>
          </el-table-column>
          <el-table-column label="第三次复习的效果" prop="reviewTimeRecall3" width="240">
            <template slot-scope="scope">
              <el-input  type="textarea" v-model="scope.row.reviewTimeRecall3" placeholder="第3次复习的效果" />
            </template>
          </el-table-column>
          <el-table-column label="第四次复习的效果" prop="reviewTimeRecall4" width="240">
            <template slot-scope="scope">
              <el-input type="textarea" v-model="scope.row.reviewTimeRecall4" placeholder="第4次复习的效果" />
            </template>
          </el-table-column>
          <el-table-column label="第五次复习的效果" prop="reviewTimeRecall5" width="240">
            <template slot-scope="scope">
              <el-input type="textarea" v-model="scope.row.reviewTimeRecall5" placeholder="第5次复习的效果" />
            </template>
          </el-table-column>
          <el-table-column label="回忆任务的创建时间" prop="createdAt" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.createdAt" type="date" value-format="yyyy-MM-dd" placeholder="请选择回忆任务的创建时间" />
            </template>
          </el-table-column>
          <el-table-column label="回忆任务的更新时间" prop="updatedAt" width="150">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.updatedAt" type="date" value-format="yyyy-MM-dd" placeholder="请选择回忆任务的更新时间" />
            </template>
          </el-table-column>
          <el-table-column label="备注" prop="remark" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.remark" placeholder="请输入备注" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReview, getReview, delReview, addReview, updateReview } from "@/api/pom/review";

export default {
  name: "Review",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedPomRecallTasks: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // review表格数据
      reviewList: [],
      // recall表格数据
      pomRecallTasksList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        tasksId: null,
        userId: null,
        content: null,
        initialStudyTime: null,
        reviewTime1: null,
        reviewTime2: null,
        reviewTime3: null,
        reviewTime4: null,
        reviewTime5: null,
        createdAt: null,
        updatedAt: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询review列表 */
    getList() {
      this.loading = true;
      listReview(this.queryParams).then(response => {
        this.reviewList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        tasksId: null,
        userId: null,
        content: null,
        initialStudyTime: null,
        reviewTime1: null,
        reviewTime2: null,
        reviewTime3: null,
        reviewTime4: null,
        reviewTime5: null,
        createdAt: null,
        updatedAt: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
        remark: null
      };
      this.pomRecallTasksList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加review";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getReview(id).then(response => {
        this.form = response.data;
        this.pomRecallTasksList = response.data.pomRecallTasksList;
        this.open = true;
        this.title = "修改review";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.pomRecallTasksList = this.pomRecallTasksList;
          if (this.form.id != null) {
            updateReview(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReview(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除review编号为"' + ids + '"的数据项？').then(function() {
        return delReview(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** recall序号 */
    rowPomRecallTasksIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** recall添加按钮操作 */
    handleAddPomRecallTasks() {
      let obj = {};
      obj.tasksId = "";
      obj.userId = "";
      obj.content = "";
      obj.initialStudyTime = "";
      obj.reviewTimeRecall1 = "";
      obj.reviewTimeRecall2 = "";
      obj.reviewTimeRecall3 = "";
      obj.reviewTimeRecall4 = "";
      obj.reviewTimeRecall5 = "";
      obj.createdAt = "";
      obj.updatedAt = "";
      obj.remark = "";
      this.pomRecallTasksList.push(obj);
    },
    /** recall删除按钮操作 */
    handleDeletePomRecallTasks() {
      if (this.checkedPomRecallTasks.length == 0) {
        this.$modal.msgError("请先选择要删除的recall数据");
      } else {
        const pomRecallTasksList = this.pomRecallTasksList;
        const checkedPomRecallTasks = this.checkedPomRecallTasks;
        this.pomRecallTasksList = pomRecallTasksList.filter(function(item) {
          return checkedPomRecallTasks.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handlePomRecallTasksSelectionChange(selection) {
      this.checkedPomRecallTasks = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pom/review/export', {
        ...this.queryParams
      }, `review_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
